<template>
  <div id="userManagePage">
    <a-flex justify="space-between">
      <h2>请假管理</h2>
    </a-flex>
    <div style="margin-bottom: 16px"></div>
    <a-form layout="inline" :model="searchParams" @finish="doSearch">

      <a-form-item label="类型" name="category">
        <a-select
          v-model:value="searchParams.category"
          :options="categoryOptions"
          placeholder="请选择请假类型"
          style="min-width: 180px"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="审核状态" name="reviewStatus">
        <a-select
          v-model:value="searchParams.reviewStatus"
          :options="LEA_REVIEW_STATUS_OPTIONS"
          placeholder="请选择审核状态"
          style="min-width: 180px"
          allow-clear
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
    </a-form>
    <div style="margin-bottom: 16px" />
    <!-- 表格 -->
    <a-table
      :columns="columns"
      :data-source="dataList"
      :pagination="pagination"
      @change="doTableChange"
    >
      <template #bodyCell="{ column, record }">
        <!-- 审核信息 -->
        <template v-if="column.dataIndex === 'reviewMessage'">
          <div>审核状态：{{ LEA_REVIEW_STATUS_MAP[record.reviewStatus] }}</div>
          <div>审核信息：{{ record.reviewMessage }}</div>
          <div v-if="record.reviewTime">审核时间：{{ dayjs(record.reviewTime).format('YYYY-MM-DD HH:mm:ss')}}</div>
        </template>
        <template v-else-if="column.dataIndex === 'createTime'">
          {{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-else-if="column.dataIndex === 'editTime'">
          {{ dayjs(record.editTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-else-if="column.key === 'action'">
          <a-space>
            <a-button v-if="record.reviewStatus!=LEA_REVIEW_STATUS_ENUM.PASS" type="link"  @click="handleReview(record,LEA_REVIEW_STATUS_ENUM.PASS)">通过</a-button>
            <a-button v-if="record.reviewStatus!=LEA_REVIEW_STATUS_ENUM.REJECT" type="link"  @click="handleReview(record,LEA_REVIEW_STATUS_ENUM.REJECT)">拒绝</a-button>
          </a-space>
        </template>
      </template>
    </a-table>
  </div>
</template>

<script lang="ts" setup>
import {message} from "ant-design-vue";
import { computed, onMounted, reactive, ref } from 'vue'
import dayjs from 'dayjs'
import {
  doLeaveReviewUsingPost,
  getLeavePageUsingPost,
  listPictureTagCategoryUsingGet
} from '@/api/leaveController.ts'
import {
  LEA_REVIEW_STATUS_ENUM,
  LEA_REVIEW_STATUS_MAP,
  LEA_REVIEW_STATUS_OPTIONS
} from '@/constants/LeaveInfo.ts'

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: '姓名',
    dataIndex: 'name',
  },
  {
    title: '部门',
    dataIndex: 'section',
  },
  {
    title: '请假类型',
    dataIndex: 'category',
  },
  {
    title: '请假事由',
    dataIndex: 'leaveReason',
    ellipsis: true,
  },
  {
    title: '前往地点',
    dataIndex: 'address',
  },
  {
    title: '审核信息',
    dataIndex: 'reviewMessage',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '编辑时间',
    dataIndex: 'editTime',
  },
  {
    title: '操作',
    key: 'action',
  },
]


// 数据
const dataList = ref([])
const total = ref(0)

// 搜索条件
const searchParams = reactive<API.LeaveQueryRequest>({
  current: 1,
  pageSize: 10,
  sortField: 'createTime',
  sortOrder: 'descend',
})

// 分页参数
const pagination = computed(() => {
  return {
    current: searchParams.current ?? 1,
    pageSize: searchParams.pageSize ?? 10,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total) => `共 ${total} 条`,
  }
})


// 获取数据
const fetchData = async () => {
  const res = await getLeavePageUsingPost({
    ...searchParams
  })
  if (res.data.data) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取数据失败，' + res.data.message)
  }
}



// 页面加载时请求一次
onMounted(() => {
  fetchData()
})

// 获取数据
const doSearch = () => {
  // 重置搜索条件
  searchParams.current = 1
  fetchData()
}

// 表格变化处理
const doTableChange = (page: any) => {
  searchParams.current = page.current
  searchParams.pageSize = page.pageSize
  fetchData()
}


const handleReview = async (record: API.LeaveInfo, reviewStatus: number) => {
  const reviewMessage = reviewStatus === LEA_REVIEW_STATUS_ENUM.PASS?'管理员通过':'管理员拒绝';
  const res= await doLeaveReviewUsingPost({
    id: record.id,
    reviewStatus,
    reviewMessage,
  })
  if(res.data.code === 0){
    message.success('审核操作成功');
    fetchData();
  } else {
    message.error('审核操作失败' + res.data.message);
  }
}

//获取请假类型
const categoryOptions = ref<string[]>([])

const getTagCategoryOptions = async () => {
  const res = await listPictureTagCategoryUsingGet()
  if (res.data.code === 0 && res.data.data) {
    categoryOptions.value = (res.data.data.categoryList ?? []).map((data: string) => {
      return {
        value: data,
        label: data,
      }
    })
  } else {
    message.error('获取分类列表失败，' + res.data.message)
  }
}

onMounted(() => {
  getTagCategoryOptions()
})

</script>



<style scoped>

</style>
